﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HP.Cachalote.DataModels;
using HP.Cachalote.Services.Interfaces;
using HP.Cachalote.Services.Validations;

namespace HP.Cachalote.Services.Validations
{
    public class TodoLabelValidator : DataValidatorBase<TodoLabel>
    {
        public const string NameErrorKey = "TodoLabelName";
        public const string IconErrorKey = "TodoLabelIcon";

        /// <summary>
        /// Validate the provided entity.
        /// </summary>
        /// <param name="entity">The instance of TodoLabel.</param>
        public override void Validate(TodoLabel entity)
        {
            CheckRequiredName(entity);
            CheckNameLength(entity);
            CheckIcon(entity);
        }

        /// <summary>
        /// Check if the required name is provided.
        /// </summary>
        /// <param name="entity">The instance of TodoLabel.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckRequiredName(TodoLabel entity)
        {
            entity.Name = entity.Name.Trim();

            if (!ValidationHelper.RuleCheckRequiredContent(entity.Name))
            {
                AddError(NameErrorKey, "Label name is required.");
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the name's max length.
        /// </summary>
        /// <param name="entity">The instance of TodoLabel.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckNameLength(TodoLabel entity)
        {
            entity.Name = entity.Name.Trim();
            int maxLength = 30;

            if (!ValidationHelper.RuleCheckMaxLength(maxLength, entity.Name))
            {
                AddError(NameErrorKey, string.Format("The length of label name exceed {0}.", maxLength));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the icon's max length.
        /// </summary>
        /// <param name="entity">The instance of TodoLabel.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckIcon(TodoLabel entity)
        {
            entity.Icon = string.IsNullOrEmpty(entity.Icon) ? string.Empty : entity.Icon.Trim();
            int maxLength = 200;

            if (!ValidationHelper.RuleCheckMaxLength(maxLength, entity.Icon))
            {
                AddError(IconErrorKey, string.Format("The length of label icon exceed {0}.", maxLength));
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
